package com.spring.sunflower.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.spring.sunflower.R;
import com.spring.sunflower.bean.LabelBean;
import com.spring.sunflower.util.DisplayUtils;

import java.util.List;

public class TagSelectAdapter extends BaseAdapter {

    private Context mContext;
    private List<LabelBean.DatasBean> mList;
    private LayoutInflater inflater;
    private int viewSize;
    private OnTagClickListener onTagClickListener;

    public TagSelectAdapter(Context context, List<LabelBean.DatasBean> list) {
        mContext = context;
        mList = list;
        inflater = LayoutInflater.from(context);
        viewSize = DisplayUtils.dpTpx(context, 90);
    }

    public void addData(List<LabelBean.DatasBean> d) {
        mList.clear();
        mList.addAll(d);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return null == mList ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.adapter_item_tag_select, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        int num = position % 4;
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) viewHolder.textView.getLayoutParams();
        layoutParams.width = viewSize;
        layoutParams.height = viewSize;
        if (mList.get(position).isChecked()) {
            switch (num) {
                case 0:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_0_selected);
                    break;
                case 1:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_1_selected);
                    break;
                case 2:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_2_selected);
                    break;
                case 3:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_3_selected);
                    break;
            }
        }else {
            switch (num) {
                case 0:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_0_normal);
                    break;
                case 1:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_1_normal);
                    break;
                case 2:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_2_normal);
                    break;
                case 3:
                    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    viewHolder.textView.setBackgroundResource(R.drawable.bg_tag_select_3_normal);
                    break;
            }
        }
        viewHolder.textView.setText(mList.get(position).getName());
        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTagClickListener != null) {
                    onTagClickListener.onTagClick(position);
                }
            }
        });
        return convertView;
    }


    class ViewHolder {
        private TextView textView;

        public ViewHolder(View view) {
            textView = view.findViewById(R.id.tag_tv);
        }


    }

    public interface OnTagClickListener {
        void onTagClick(int position);
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        this.onTagClickListener = onTagClickListener;
    }
}